(function($) {
$(document).ready(function() {
	var validator = $("#add_form").validate({
		rules: {
			'ordersshared[shared_tarif]': {
				required: true
			},
			'ordersshared[terms]': {
				required: true
			},
			'domain': {
				required: true
			}
		},
		messages: {
			'ordersshared[shared_tarif]': {
				required: "Выберите тариф"
			},
			'ordersshared[terms]': {
				required: "Согласитесь с условиями"
			},
			'domain': {
				required: "Выберите тип домена"
			}
		},

		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},

		submitHandler: function(form) {
			$.ajax(
			{
				 type: 'POST',
				 url: "./shared_add",
				 data:$('#add_form').serialize(),
				 cache: false,
				 success: function(html)
				 {
					location.href = 'panel#users_bills:bill=' + html;
				 }
			});
		},
 
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
		

	});
	
	$.validator.addMethod("check_new_domain", function(value, element) 
	{
		var check_new_domain = $.ajax({ type: "GET", url: "./shared_check_new_domain/"+value, async: false }).responseText;
		if (check_new_domain != "true")
		{
			$.validator.messages.check_new_domain = check_new_domain;
			return false;
		}
		else
			return true;
	}
	);
	
	$.validator.addMethod("check_transfer_domain", function(value, element) 
	{
		var check_transfer_domain = $.ajax({ type: "GET", url: "./shared_check_transfer_domain/"+value, async: false }).responseText;
		if (check_transfer_domain != "true")
		{
			$.validator.messages.check_transfer_domain = check_transfer_domain;
			return false;
		}
		else
			return true;
	}
	);
});

})(jQuery);
